package org.gongliang.jsmile.authc.sys.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.gongliang.jsmile.authc.sys.entity.SysResources;
import org.gongliang.jsmile.authc.sys.service.SysResourcesService;
import org.gongliang.jsmile.common.base.BaseController;
import org.gongliang.jsmile.common.base.JSmileResult;
import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.gongliang.jsmile.common.kit.web.WebKit;
import org.gongliang.jsmile.config.log.info.KLog;
import org.gongliang.jsmile.config.log.info.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @desc   控制器
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 04:57:24
*/
@Controller
@RequestMapping("/sys_resources")
@Api(tags = "资源管理")
public class SysResourcesController extends BaseController {

    @Autowired
    private SysResourcesService sysResourcesService;
    
	@GetMapping("/main")
	public String main() {
		return "sys/Resources/res";
	}
	@GetMapping("/add")
	public String add() {
		return "sys/Resources/add";
	}
	@GetMapping("/edit")
	public String edit() {
		return "sys/Resources/edit";
	}


	@ResponseBody
	@RequestMapping("/findAll")
	@ApiOperation(value = "获取资源列表", notes = "获取角色列表")
	@KLog(title = "资源管理->获取资源列表", type = LogType.QUERY)
	public JSmileResult findAll(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		List<SysResources> list = sysResourcesService.findAll(reqMap);
		JSmileResult result = JSmileResult.success();
		result.setResData(list);
		return result;
	}

	@KLog(title = "资源管理->新增资源", type = LogType.INSERT)
	@RequiresPermissions("sys:resources:add")
	@PostMapping("/save")
	@ResponseBody
	public JSmileResult save(SysResources resources) {
		JSmileResult result=JSmileResult.success("保存资源信息成功!");
		resources.setCreateDate(new Date());
		resources.setCreator(ShiroKit.getUserId());
		if(!sysResourcesService.saveORupdate(resources)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("保存资源信息失败");
		}
		return result;
	}
	@KLog(title = "资源管理->修改资源信息", type = LogType.UPDATE)
	@RequiresPermissions("sys:resources:update")
	@PostMapping("/update")
	@ResponseBody
	public JSmileResult update(SysResources resources) {
		JSmileResult result=JSmileResult.success("修改资源信息成功!");
		resources.setUpdater(ShiroKit.getUserId());
		resources.setUpdateDate(new Date());
		if(!sysResourcesService.saveORupdate(resources)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("修改资源信息失败");
		}
		return result;
	}


	/**
	 * 根据一组id删除
	 */
	@KLog(title = "资源管理->删除资源信息", type = LogType.DELETE)
	@RequiresPermissions("sys:resources:delete")
	@ResponseBody
	@PostMapping("/deleteByIds")
	public JSmileResult deleteByIds(@RequestParam(value = "ids[]", required = true) String[] ids) {
		if (ids == null || ids.length == 0) {
			return JSmileResult.error(KException.IDS_NOT_NULL);
		}
		return sysResourcesService.deleteResources(ids) ? JSmileResult.success("删除资源成功!") : JSmileResult.error("删除资源失败!");
	}

	@RequiresPermissions("sys:resources:saveBasePermission")
	@ResponseBody
	@PostMapping("/saveBasePermission")
	@KLog(title = "资源管理->添加基本权限", type = LogType.INSERT)
	public JSmileResult saveBasePermission(@RequestParam(value = "parentId", required = true) Integer parentId, Integer level) {
		return sysResourcesService.saveBasePermission(parentId, level) ? JSmileResult.success("添加基本权限成功!")
				: JSmileResult.error("添加基本权限失败!");
	}
}
